package com.bridgeintelligent.tag.scene.mapper;

import com.bridgeintelligent.tag.scene.api.vo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @description
 * @author: grp 2021-01-10 22:29
 **/
@Mapper
public interface SceneFuncMapper {

    @Select("select customers_id id, customers_name name from tag_customers a WHERE a.IS_DEL = '00' and a.APPROVAL_STATUS = '4' and a.d_status = '01' \n" +
            "                    and (a.OWNER_ID = #{userId} OR\n" +
            "                    (a.OWNER_ID <> #{userId}\n" +
            "                    and (a.shared_state = '01' or (\n" +
            "                    a.shared_state = '02' and exists(select 1 from TAG_CUSTOMERS_SHARE_RELATION r \n" +
            "                    where r.customers_id = a.customers_id and r.user_id = #{userId} and r.is_share = '01'))))) \n" +
            "                    order by a.upt_time desc")
    List<FuncVO> findCustomers(String userId);

    @Select("select dim_id id, dim_name name from dim_data a where a.DIM_LARGE_TYPE = '04' and a.state = '01' " +
            " and (a.CREATER_ID = #{userId} or a.SHARED_STATE = '01' or (a.SHARED_STATE = '02' and (" +
            " (a.CREATER_ID <> #{userId} and exists(select 1 from TAG_SHARE_RELATION r where" +
            " r.dim_id = a.dim_id and r.user_id = #{userId})))))" +
            " order by a.upt_time desc")
    List<FuncVO> findCombinations(String userId);

    @Select("select dim_id id, dim_name name from dim_data a where DIM_LARGE_TYPE = '02' and a.approve_state = '4' " +
            " and a.state = '01' and (a.CREATER_ID = #{userId} or a.SHARED_STATE = '01' or (a.SHARED_STATE = '02' and (" +
            " (a.CREATER_ID <> #{userId} and exists(select 1 from TAG_SHARE_RELATION r where" +
            " r.dim_id = a.dim_id and r.user_id = #{userId})))))" +
            " order by a.upt_time desc")
    List<FuncVO> findFrees(String userId);

    @Select("select id, al_name name from TAG_ANALYSIS_EXPLORE a where ct_name = #{userId} order by ut_time desc")
    List<FuncVO> findExplores(String userId);

    @Select(" SELECT\n" +
            "  t5.id id,\n" +
            "  t5.customer_id cid,\n" +
            "  t5.export_name name,\n" +
            "  t5.instance_id instanceId,t5.user_id userId\n" +
            " FROM\n" +
            "  tag_workflow_approver t1\n" +
            " LEFT JOIN tag_workflow_node t2 ON\n" +
            "  t1.node_id = t2.id\n" +
            " LEFT JOIN tag_workflow_instance t3 ON\n" +
            "  t2.workflow_id = t3. workflow_id\n" +
            "  AND t2.sort = t3.node_order\n" +
            "  AND t3.current_status = '4'\n" +
            " LEFT JOIN tag_customers t4 ON\n" +
            "  t4.customers_id = t3.d_resource\n" +
            " LEFT JOIN tag_customers_export t5 ON\n" +
            "  t5.customer_id = t4.customers_id\n" +
            "  AND t5.instance_id = t3.id\n" +
            "  AND t5.user_id = #{userId}\n" +
            " WHERE\n" +
            "  t3.workflow_id IS NOT NULL\n" +
            "  AND t4.customers_id IS NOT NULL\n" +
            "  AND t5.id IS NOT NULL\n" +
            " ORDER BY t5.upt_time DESC\n")
    List<FuncVO> findExports(String userId);

    @Select("select id,LOOK_NAME name from TAG_LOOKLIKE_INFO where is_del = '00' and user_id = #{userId} order by create_time desc")
    List<FuncVO> findLooklikes(String userId);

    @Select("select id,CUSTOMER_NAME as name,INDEX_ID as indexId from TAG_TRACK_INFO where is_del = '0' and create_id = #{userId} order by create_time desc")
    List<FuncVO> findCustomersTracks(String userId);
}
